package com.gis.interceptor;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LnnuUser
 * @create 2021-08-03-下午8:16
 */
public class TypeInterceptor implements Interceptor {

    //声明一个存放事件的集合
    private ArrayList<Event> addHeaderEvents;

    @Override
    public void initialize() {

        //初始化存放事件的集合
        addHeaderEvents = new ArrayList<>();
    }

    /**
     * 处理单个事件
     * @param event
     * @return
     */
    @Override
    public Event intercept(Event event) {

        //1、获取头信息
        Map<String, String> headers = event.getHeaders();

        //2、获取body
        String body = new String(event.getBody());

        //3、根据body中是否有“hello”来决定添加怎样的头信息
        if (body.contains("hello")){

            //4、添加头信息
            headers.put("type","GIS");
        }else {

            //4、添加头信息
            headers.put("type","AIS");
        }

        return event;
    }

    @Override
    public List<Event> intercept(List<Event> list) {

        //1、清空集合
        addHeaderEvents.clear();

        //2、遍历events，给每一个事件添加头信息
        for (Event event : list) {

            //3、给每一个事件添加头信息
            addHeaderEvents.add(intercept(event));

        }

        //4、返回结果
        return addHeaderEvents;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder{

        @Override
        public Interceptor build() {

            return new TypeInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
